package cn.yj.core.business.service;

import cn.yj.annotation.pagehelper.page.Page;
import cn.yj.cgs.dba.entity.PartTimeJob;
import cn.yj.cgs.dba.entity.vo.PartTimeJobVo;
import cn.yj.cgs.dba.mapper.PartTimeJobMapper;
import cn.yj.common.Enums;
import cn.yj.common.UUIdUtils;
import cn.yj.commons.utils.StringUtils;
import cn.yj.core.business.service.iservice.IMessageService;
import cn.yj.core.business.service.iservice.IPartTimeJob;
import cn.yj.core.kwfilter.KeyWorkFilterService;
import cn.yj.entity.R;
import cn.yj.params.check.annotation.CheckObjectValue;
import cn.yj.params.check.annotation.KeyValue;
import cn.yj.params.check.annotation.Require;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 永健
 * @since 2021-09-28 11:49
 */
@Service
public class PartTimeJobService extends ServiceImpl<PartTimeJobMapper, PartTimeJob> implements IPartTimeJob {

    @Resource
    private IMessageService messageService;

    @Resource
    private CustomerService customerService;


    @CheckObjectValue(keyValue = @KeyValue(type = PartTimeJob.class, name = {"userCode", "price", "content"}))
    @Override
    public R saveInfo(PartTimeJob entity) {
        // 敏感词过滤
        String msg = filterWord(entity);
        entity.setContent(entity.getContent().replaceAll("\n", "<br/>"));
        entity.setJobCode(UUIdUtils.getPartTimeCode());
        return super.save(entity)? R.success(msg) : R.error("保存失败");
    }

    @CheckObjectValue(keyValue = @KeyValue(type = PartTimeJob.class, name = {"jobCode", "userCode", "price", "content"
    }))
    @Override
    public R updateInfoById(PartTimeJob entity) {
        // 敏感词过滤
        String msg = filterWord(entity);
        entity.setContent(entity.getContent().replaceAll("\n", "<br/>"));
        entity.setUpdateTime(new Date());
        return baseMapper.update(entity, new UpdateWrapper<PartTimeJob>().lambda().eq(PartTimeJob::getJobCode,
                entity.getJobCode())) > Enums.Common.ZERO? R.success(msg) : R.error("更新失败");
    }


    private String filterWord(PartTimeJob entity) {
        Set<String> words = KeyWorkFilterService.filter_search_out_word(entity.getContent(),
                customerService.getOpenIdByUserCode(entity.getUserCode()));
        String msg = "信息已发布，回去看看";
        entity.setAuditStatus(Enums.Article.STATUS.SUCCESS.name());
        if (!words.isEmpty()) {
            entity.setAuditStatus(Enums.Article.STATUS.DOING.name());
            msg = "您的兼职招聘内容疑似存在敏感词 [ " + StringUtils.join(words, ",") + "],文章需要进行人工核实，方可正常发布! 请同学用语文明 " + "否则账号会被封禁～";
        }
        return msg;
    }


    @Override
    public Page<PartTimeJobVo> pageList(@Require Map<String, Object> params, @Require Page<PartTimeJobVo> page) {
        params.put("auditStatus", Enums.Article.STATUS.SUCCESS.name());
        params.put("deleted", Enums.Common.ZERO);
        baseMapper.pageList(params, page);
        return page;
    }

    @Override
    public Page<PartTimeJobVo> myPushPageList(@Require Map<String, Object> params, @Require Page<PartTimeJobVo> page) {
        params.put("deleted", Enums.Common.ZERO);
        baseMapper.pageList(params, page);
        return page;
    }

    @Override
    public boolean deleteInfoByCode(@Require String jobCode) {
        return baseMapper.delete(new UpdateWrapper<PartTimeJob>().lambda().eq(PartTimeJob::getJobCode, jobCode)) > Enums.Common.ZERO;
    }

    @Override
    public PartTimeJobVo detailByCode(String jobCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("jobCode", jobCode);
        List<PartTimeJobVo> list = baseMapper.list(params);
        PartTimeJobVo partTimeJobVo = list.get(Enums.Common.ZERO);
        partTimeJobVo.setContent(partTimeJobVo.getContent().replaceAll("<br/>", "\n"));
        return list.isEmpty()? null : partTimeJobVo;
    }
}
